<?php
function show_page() {
	global $_GET, $_POST, $data, $path, $style, $session_id, $language, $term_Thing_ID, $header;
	if (!isset($_GET["act"])) $_GET["act"] = "page";
	switch ($_GET["act"]) {
		case "res":
			global $_GET, $path, $style, $session_id, $data, $language;
			$filename = $path[$style]["tpl"]."/userdata.xml";
			$fp = fopen($filename, "r");
			$contents = fread($fp, filesize ($filename));
			fclose ($fp);
			
			preg_match("/\%REPEAT\%(.*?)\%REPEAT\%/s", $contents, $matches);
			preg_match("/\%REPDAT\%(.*?)\%REPDAT\%/s", $matches[1], $datmatches);
			preg_match("/\%REPOPT\%(.*?)\%REPOPT\%/s", $datmatches[1], $optmatches);
			$rep_cont = "";
			
			switch (substr($_GET["typeo"], 1)) {
				case 4:
					$typename = getMessage("Domain Ontology")." + ".getMessage("Domain Ontology");
					$ftest = 0;
					$stest = 0;
				break;
			}
			
			// Type
			$otype = str_replace($optmatches[0], "", $datmatches[1]);
			$otype = str_replace("%TYPE%", "Label", $otype);
			$otype = str_replace("%NODE_ID%", "", $otype);
			$otype = str_replace("%NODE_TL%", "", $otype);
			$otype = str_replace("%VALUE%", $typename, $otype);
			
			$udata = str_replace($datmatches[0], $otype, $matches[1]);
			$udata = str_replace("%GROUP_ID%", "0", $udata);
			$udata = str_replace("%GROUP_TL%", "", $udata);
			
			if ($ftest == 0) $usdata = "";  
			else {	//First onto
				$fonto = str_replace($optmatches[0], "", $datmatches[1]);
				$fonto = str_replace("%TYPE%", "Label", $fonto);
				$fonto = str_replace("%NODE_ID%", "", $fonto);
				$fonto = str_replace("%NODE_TL%", "1:	", $fonto);
				$fonto = str_replace("%VALUE%", $fname, $fonto);
				
				$usdata = str_replace($datmatches[0], $fonto, $matches[1]);
				$usdata = str_replace("%GROUP_ID%", "1", $usdata);
				$usdata = str_replace("%GROUP_TL%", "", $usdata);
			}
			
			if ($stest == 0) $usdata1 = ""; 
			else {	//Second onto
				$sonto = str_replace($optmatches[0], "", $datmatches[1]);
				$sonto = str_replace("%TYPE%", "Label", $sonto);
				$sonto = str_replace("%NODE_ID%", "", $sonto);
				$sonto = str_replace("%NODE_TL%", "2:	", $sonto);
				$sonto = str_replace("%VALUE%", $sname, $sonto);
				
				$usdata1 = str_replace($datmatches[0], $sonto, $matches[1]);
				$usdata1 = str_replace("%GROUP_ID%", "2", $usdata1);
				$usdata1 = str_replace("%GROUP_TL%", "", $usdata1);
			}
			
			//Button
			$ubutn = str_replace($optmatches[0], "", $datmatches[1]);
			$ubutn = str_replace("%TYPE%", "Link", $ubutn);
			$ubutn = str_replace("%NODE_ID%", "", $ubutn);
			$ubutn = str_replace("%NODE_TL%", getMessage("Enter"), $ubutn);
			$ubutn = str_replace("%VALUE%", "index.php?trg=merger&amp;sid=".$session_id."&amp;typeo=".substr($_GET["typeo"],1), $ubutn);
			//User
			$uldata = str_replace($datmatches[0], $ubutn, $matches[1]);
			$uldata = str_replace("%GROUP_ID%", "3", $uldata);
			$uldata = str_replace("%GROUP_TL%", "", $uldata);
			
			$contents = str_replace($matches[0], $udata.$usdata.$usdata1.$uldata, $contents);
			
			header ($header["xml"]);    
		break;
		
		default: //user's data
			if ($_REQUEST['act'] ==  'del') {
				foreach ($_REQUEST['mid'] as $iMergingId) {
					$sQuery = "DELETE FROM merging WHERE id = ".$iMergingId;
					insertRecord($sQuery);
					$sQuery = "DELETE FROM merging_data WHERE merging_id = ".$iMergingId;
					insertRecord($sQuery);	
				}
			}
			$filename = $path[$style]["tpl"]."/merger_choise.htm";
			$fp = fopen($filename, "r");
			$contents = fread($fp, filesize ($filename));
			fclose ($fp);
			
			$contents = str_replace("%ROOT_CSS%", $path[$style]["css"], $contents);
			$contents = str_replace("%ROOT_IMG%", $path[$style]["img"], $contents);
			$contents = str_replace("%ROOT_JAVA%", $path[$style]["java"], $contents);
			
			$contents = str_replace("%SID%", $session_id, $contents);  
			$contents = str_replace("%TRG%", "merger_choise", $contents);
			$contents = str_replace("%LOAD%", getMessage("Loading"), $contents);
			$contents = str_replace("%UNAVAIL%", getMessage("Unavailable"), $contents);
			
			$contents = str_replace("%LOGOUT%", getMessage("Logout"), $contents);
			$contents = str_replace("%MAINMENU%", getMessage("Main menu"), $contents);
			$contents = str_replace("%MERG%", getMessage("Ontology merger"), $contents);
			$contents = str_replace("%F_ONTO%", getMessage('First ontology'), $contents);
			$contents = str_replace("%S_ONTO%", getMessage('Second ontology'), $contents);
			
			$contents = str_replace("%Enter name of the new ontology:%", getMessage("Enter name of the new ontology:"), $contents);
			$contents = str_replace("%Next &gt;%", getMessage("Next &gt;"), $contents);
			$contents = str_replace('%Input name!%', getMessage('Input name!'), $contents);
			$contents = str_replace('%Select Type!%', getMessage('Select Type!'), $contents);
			$contents = str_replace('%Select type of merging%', getMessage('Select type of merging'), $contents);
			
			$contents = str_replace('%NAME%', $_REQUEST['name'], $contents);
			$contents = str_replace('%ERROR%', $_REQUEST['error'], $contents);
			
			
			$sTypes =
				ShowType(1, getMessage('Domain + Domain')) .
				ShowType(2, getMessage('Domain + Task and Method')) .
				ShowType(3, getMessage('Task and Method + Task and Method')) .
				ShowType(4, getMessage('Application + Application')) .
				ShowType(5, getMessage('Extend Application ontology with Domain'), true) .
				ShowType(6, getMessage('Extend Application ontology with Task and Method Ontology'), true)
				;

			$contents = str_replace("%TYPES%", $sTypes, $contents);
			$contents = str_replace("%CREATED%", ShowCreated(), $contents);
			
			header ($header["html"]);
		break;
	}
	
	header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT");    // Date in the past
	header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // always modified
	header ("Cache-Control: no-cache, must-revalidate");  // HTTP/1.1
	header ("Pragma: no-cache");                          // HTTP/1.0
	
	print ($contents);
}

function ShowType ($iResult, $sTitle, $disableInput = false) {
	global $style, $path;
	return  '<div id="__'.$iResult.'">
    	<img id="p'.$iResult.'" src="'.$path[$style]["img"].'/Empty.gif">
    	<span id="s'.$iResult.'" class="notSelected">
    		<input type=radio name=typeo value="'.$iResult.'" onclick="iType = '.$iResult.';'.($disableInput ? '$(\'name_block\').style.visibility = \'hidden\';' : '$(\'name_block\').style.visibility = \'visible\';').'">'.$sTitle.'
		</span>
	</div>'."\n";
}

function ShowCreated () {
	global $style, $path, $language;
	$sQuery = "SELECT * FROM session WHERE session_id = '".mysql_escape_string($_GET['sid'])."'";
	getData($sQuery, $aSession);
	
	$sQuery = "SELECT * FROM merging WHERE user_id = ".$aSession['user_id'];
	$i_num = getMultData($sQuery, $aMerging);
	
	ob_start();
	if ($_REQUEST['result_message']) {
		echo "<center><strong>".$_REQUEST['result_message']."</strong></center>";
	}
	if ($i_num) {
		echo "<p><strong>".getMessage('Created, but not completed').":</strong></p>
		<form method=post onsubmit='return confirm (\"".getMessage('Do you really want to delete the incomplete ontologys?')."\");'>
		<input type=hidden name=sid value='{$aSession['session_id']}'>
		<input type=hidden name=act value='del'>
		";
		for ($i = 0; $i < $i_num; $i++) {
			if ($aMerging[$i]['merging_type'] == 'external') {
				if (!$aMerging[$i]['external_onto_id']) {
					$aMerging[$i]['name'] = '<span style=\'color:red;\'>'.getMessage('Onotology without name!').'</span>';
				} else {
					$sQuery = "SELECT `text` as name FROM ontology as o
					INNER JOIN term as t ON (o.term_id = t.id)
					WHERE lang_id = '".$language["content"]."' AND o.id = ".$aMerging[$i]['external_onto_id'];
					getData ($sQuery, $aOntology);
					$aMerging[$i]['name'] = getMessage('Extend')." ".$aOntology['name'];
				}
			}
			
			echo "
			<div>
			<img src=".$path[$style]["img"]."/ontology.png border=0>
			<input type=checkbox name=mid[] value='{$aMerging[$i]['id']}'> <span class=notSelected onclick='this.className = \"selected\"'>
				<a href='index.php?trg=merger&sid=".$aSession['session_id']."&merging_id=".$aMerging[$i]['id']."'>".$aMerging[$i]['name']."</a>
			</span>
			</div>
			";
		}
		echo "<br>&nbsp;<input type=submit value='".getMessage('DELETE')."'></form>";
	}
	return ob_get_clean();
}
?>